<template>
  <el-dialog title="分配角色" :visible="isShowAssignRole" width="40%" @close="onClose">
    <el-checkbox-group v-model="checkedList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="mini" @click="onClick">确认</el-button>
        <el-button type="info" size="mini" @click="onClose">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getSysRoleId, getSysRoleListEnabled, putSysUserAssignRoles } from '@/api/employee/employee'
export default {
  name: 'AssignRole',
  props: {
    isShowAssignRole: {
      type: Boolean,
      default: false
    },
    id: {
      type: [Number, String],
      default: null
    }
  },
  data() {
    return {
      roleList: [],
      checkedList: []
    }
  },
  watch: {
    id: {
      async handler(newValue) {
        const res = await getSysRoleId(newValue)
        this.checkedList = res.permIds
      }
    }
  },
  async mounted() {
    this.roleList = await getSysRoleListEnabled()
  },
  methods: {
    onClose() {
      this.$emit('update:isShowAssignRole', false)
    },
    async onClick() {
      try {
        await putSysUserAssignRoles({ id: this.id, roleIds: this.checkedList })
        this.$message.success('分配角色成功')
        this.onClose()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
